import { Form } from "@b/model/base/form";
import { List } from "@b/model/base/list";
import { ListSearch } from "@b/model/base/list_search";
import type { interForm, listButton } from "@b/model/base/type";
import { NURSING_APPOINTMENT_STATUS } from "@b/model/enum";
import { pagebuttonStore } from "@b/store";
import { h, reactive, type VNode } from "vue";

export class IListSearch extends ListSearch<interNursingAppointment & { times: [Date, Date] }> {
    constructor() {
        super()
        this.reactiveProp = ['times', 'status']
        this.labels = {
            userName: {
                prop: "userName",
                label: "用户姓名",
                type: 'text',
                clearable: true,
            },
            userPhone: {
                prop: "userPhone",
                label: "用户手机号",
                type: 'text',
                clearable: true,
            },
            times: {
                prop: "times",
                label: "预约时间",
                type: 'date',
                dateType: 'daterange',
                clearable: true,
            },
            status: {
                prop: "status",
                label: "预约状态",
                type: 'select',
                options: Object.entries(NURSING_APPOINTMENT_STATUS).map(([key, value]) => ({
                    label: key,
                    value: value
                })),
                clearable: true,
            }
        }
    }
    init() {

    }
}

export class IList extends List<interNursingAppointment> {
    constructor() {
        super()
        this.title = "订单列表"
        this.baseUrl = '/nursing_appointment'
        this.labels = {
            id: { prop: "id", label: "预约ID", type: 'text' },
            userName: { prop: "userName", label: "用户姓名", type: 'text' },
            userPhone: { prop: "userPhone", label: "用户手机号", type: 'text' },
            address: { prop: "address", label: "预约地点", type: 'text' },
            preTime: { prop: "preTime", label: "预约时间", type: 'text' },
            preDepartmentName: { prop: "preDepartmentName", label: "意向科室", type: 'text' },
            preNurseName: { prop: "preNurseName", label: "意向护理人", type: 'text' },
            statusName: { prop: "statusName", label: "预约状态", type: 'text' },
        }
        this.select = undefined
        this.editButton = () => {
            const but = pagebuttonStore().button
            const buttons: VNode[] = []
            if (but.has('nursing_appointment:add')) {
                buttons.push(h('div', { className: 'textbutton', onClick: this.add }, but.get('nursing_appointment:add')))
            }
            return buttons
        }
        this.actions = ({ data }) => {
            const actions: listButton<interNursingAppointment>[] = []
            const but = pagebuttonStore().button
            if (but.has('nursing_appointment:info')) {
                actions.push({
                    text: but.get('nursing_appointment:info') || '详情',
                    onClick: (data) => {
                        this.info(data.id)
                    }
                })
            }
            if (but.has('nursing_appointment:delete')) {
                actions.push({
                    text: but.get('nursing_appointment:delete') || '删除',
                    type: 'danger',
                    hasConfirm: true,
                    onClick: (data, index) => {
                        this.deleteItem(index)
                    }
                })
            }
            if (but.has('nursing_appointment:depart') && data.status === NURSING_APPOINTMENT_STATUS.已分配) {
                actions.push({
                    text: but.get('nursing_appointment:depart') || '出发',
                    hasConfirm: true,
                    confirmText: '确认出发吗？',
                    onClick: (data) => {
                        this.depart(data.id)
                    }
                })
            }
            if (but.has('nursing_appointment:arrive') && data.status === NURSING_APPOINTMENT_STATUS.已出发) {
                actions.push({
                    text: but.get('nursing_appointment:arrive') || '到达',
                    hasConfirm: true,
                    confirmText: '确认到达吗？',
                    onClick: (data) => {
                        this.arrive(data.id)
                    }
                })
            }
            if (but.has('nursing_appointment:nursing') && data.status === NURSING_APPOINTMENT_STATUS.已到达) {
                actions.push({
                    text: but.get('nursing_appointment:nursing') || '开始',
                    hasConfirm: true,
                    confirmText: '确认开始护理吗？',
                    onClick: (data) => {
                        this.nursing(data.id)
                    }
                })
            }
            if (but.has('nursing_appointment:complete') && data.status === NURSING_APPOINTMENT_STATUS.护理中) {
                actions.push({
                    text: but.get('nursing_appointment:complete') || '完成',
                    hasConfirm: true,
                    confirmText: '确认完成护理吗？',
                    onClick: (data) => {
                        this.complete(data.id)
                    }
                })
            }
            return actions
        }
    }
    async info(_id?: number) {
        console.log(_id)
        throw new Error('未设置详情函数')
    }
    add(_id?: number) {
        console.log(_id)
        throw new Error('未设置新增函数')
    }
    async depart(id?: number) {
        const res = await this.request('/nursing_appointment/depart', { data: { id } })
        if (res) {
            this.refresh()
        }
    }
    async arrive(id?: number) {
        const res = await this.request('/nursing_appointment/arrive', { data: { id } })
        if (res) {
            this.refresh()
        }
    }
    async nursing(id?: number) {
        const res = await this.request('/nursing_appointment/nursing', { data: { id } })
        if (res) {
            this.refresh()
        }
    }
    async complete(id?: number) {
        const res = await this.request('/nursing_appointment/complete', { data: { id } })
        if (res) {
            this.refresh()
        }
    }
}

export class NurseLeaderList extends IList {
    constructor() {
        super()
        this.title = "订单列表"
        this.url.page = '/nursing_appointment/list_by_nurse_leader'
        this.labels = {
            userPhone: { prop: "userPhone", label: "用户手机号", type: 'text' },
            address: { prop: "address", label: "预约地点", type: 'text' },
            preDepartmentName: { prop: "preDepartmentName", label: "意向科室", type: 'text' },
            nurseName: { prop: "nurseName", label: "意向护理人", type: 'text' },
            statusName: { prop: "statusName", label: "预约状态", type: 'text' },
        }
        this.actions = () => {
            const actions: listButton<interNursingAppointment>[] = []
            const but = pagebuttonStore().button
            if (but.has('nursing_appointment:info:history:nurse')) {
                actions.push({
                    text: but.get('nursing_appointment:info:history:nurse') || '详情',
                    onClick: (data) => {
                        this.info(data.id)
                    }
                })
            }
            return actions
        }
    }
}
export class NurseAcceptList extends IList {
    constructor() {
        super()
        this.title = "待接受列表"
        this.hasPages = false
        this.url.list = '/nursing_appointment/list_accept_by_nurse'
        this.labels = {
            userPhone: { prop: "userPhone", label: "用户手机号", type: 'text' },
            address: { prop: "address", label: "预约地点", type: 'text' },
            preTime: { prop: "preTime", label: "预约时间", type: 'text' },
            statusName: { prop: "statusName", label: "预约状态", type: 'text' },
        }
        this.actions = () => {
            const actions: listButton<interNursingAppointment>[] = []
            const but = pagebuttonStore().button
            if (but.has('nursing_appointment:info:nurse')) {
                actions.push({
                    text: but.get('nursing_appointment:info:nurse') || '详情',
                    onClick: (data) => {
                        this.info(data.id)
                    }
                })
            }
            if (but.has('nursing_appointment:accept')) {
                actions.push({
                    text: but.get('nursing_appointment:accept') || '接受',
                    type: 'primary',
                    hasConfirm: true,
                    confirmText: '确认接受吗？',
                    onClick: (data) => {
                        this.accept(data.nurseOrderId)
                    }
                })
            }
            if (but.has('nursing_appointment:accept:no')) {
                actions.push({
                    text: but.get('nursing_appointment:accept:no') || '拒绝',
                    type: 'danger',
                    hasConfirm: true,
                    confirmText: '确认拒绝吗？',
                    onClick: (data) => {
                        this.reject(data.nurseOrderId)
                    }
                })
            }
            return actions
        }
    }
    async accept(id?: number) {
        const res = await this.request('/nursing_appointment/accept_by_nurse', { data: { id } })
        if (res) {
            this.refresh()
        }
    }
    async reject(id?: number) {
        const res = await this.request('/nursing_appointment/reject_by_nurse', { data: { id } })
        if (res) {
            this.refresh()
        }
    }
}
export class NurseList extends IList {
    constructor() {
        super()
        this.title = "今日订单"
        this.hasPages = false
        this.url.list = '/nursing_appointment/list_by_nurse'
        this.labels = {
            userPhone: { prop: "userPhone", label: "用户手机号", type: 'text' },
            address: { prop: "address", label: "预约地点", type: 'text' },
            preTime: { prop: "preTime", label: "预约时间", type: 'text' },
            statusName: { prop: "statusName", label: "预约状态", type: 'text' },
        }
        this.actions = () => {
            const actions: listButton<interNursingAppointment>[] = []
            const but = pagebuttonStore().button
            if (but.has('nursing_appointment:info:nurse')) {
                actions.push({
                    text: but.get('nursing_appointment:info:nurse') || '详情',
                    onClick: (data) => {
                        this.info(data.id)
                    }
                })
            }
            return actions
        }
    }
}
export class NurseHistoryList extends IList {
    constructor() {
        super()
        this.title = "订单列表"
        this.url.page = '/nursing_appointment/history_by_nurse'
        this.actions = () => {
            const actions: listButton<interNursingAppointment>[] = []
            const but = pagebuttonStore().button
            if (but.has('nursing_appointment:info:history:nurse')) {
                actions.push({
                    text: but.get('nursing_appointment:info:history:nurse') || '详情',
                    onClick: (data) => {
                        this.info(data.id)
                    }
                })
            }
            return actions
        }
    }
}
export class NurseLeaderHistoryList extends IList {
    constructor() {
        super()
        this.title = "订单列表"
        this.url.page = '/nursing_appointment/history_by_nurse_leader'
    }
}

export class IForm extends Form<interNursingAppointment> {
    labels_info: { [key in keyof interNursingAppointment & string]?: interForm<interNursingAppointment, key> } = reactive({});
    departmentTree: { id: number, value: number, departments: interDepartment[] }[] = [];
    nurse: { id: number, name: string }[] = [];
    nurseLeader: { id: number, name: string }[] = [];
    nurseLeaderId: number = 0;
    nurseIds: number[] = [];
    showAssign: boolean = false;
    constructor() {
        super()
        this.title = "订单详情"
        this.baseUrl = '/nursing_appointment'
        this.label_width = '7em'
    }
    _setLabels(departmentOptions: { label: string, value: number }[]) {
        this.setItem({
            userName: { prop: "userName", label: "用户姓名", type: 'text' },
            userPhone: { prop: "userPhone", label: "用户手机号", type: 'text' },
            preTime: { prop: "preTime", label: "预约时间", type: 'text' },
            address: { prop: "address", label: "预约地点", type: 'text' },
            preDepartmentId: {
                prop: "preDepartmentId", label: "意向科室", type: 'select', options: departmentOptions, selectName: 'preDepartmentName', onChange: async ({ value, item }) => {
                    this.data.preDepartmentName = item.label
                    const res = await this.request('/user/select', { parames: { departmentId: value }, cache: 'global' })
                    this.setItem({
                        preNurseId: {
                            prop: "preNurseId", label: "意向护理人", type: 'select', options: res.map((item: { name: string, id: number }) => {
                                return {
                                    label: item.name,
                                    value: item.id
                                }
                            }), onChange: async ({ value, item }) => {
                                this.data.preNurseName = item.label
                            }
                        },
                    }, this.labels)
                }
            },
            preNurseId: {
                prop: "preNurseId", label: "意向护理人", type: 'select', options: [], selectName: 'preNurseName'
            },
        }, this.labels)
        this.setItem({
            orderNo: { prop: "orderNo", label: "订单编号", type: 'text' },
            projectName: { prop: "projectName", label: "项目名称", type: 'text' },
            projectPrice: { prop: "projectPrice", label: "项目金额", type: 'text' },
            servicePrice: { prop: "servicePrice", label: "上门服务费", type: 'text' },
            payTime: { prop: "payTime", label: "支付时间", type: 'text' },
            auditTime: { prop: "auditTime", label: "审核时间", type: 'text' },
            statusName: { prop: "statusName", label: "预约状态", type: 'text' },
        }, this.labels_info)
    }
    async getDepartmentOptions() {
        return (await this.request('/department/select/child', { method: 'GET', cache: 'global' })).map((item: { name: string, id: number }) => {
            return {
                label: item.name,
                value: item.id
            }
        })
    }
    async getDepartmentTree() {
        this.departmentTree = await this.request('/department/tree', { method: 'GET', cache: 'global' })
    }
    async getNurseOptions() {
        this.nurse = await this.request('/user/select/nurse', { cache: 'global' })
    }
    async handleDepartmentClick(id: number) {
        this.nurseLeader = await this.request('/user/select/nurse/leader', { cache: 'global', parames: { departmentId: id } })
    }
    async init() {
        const options = await this.getDepartmentOptions()
        this._setLabels(options)
    }
    async pay() {
        const res = await this.request('/nursing_appointment/pay', { method: 'POST', data: { id: this.id } })
        if (res) this.setData()
    }
    async audit() {
        const res = await this.request('/nursing_appointment/audit', { method: 'POST', data: { id: this.id, nurseId: this.nurseLeaderId } })
        this.show = false
        if (res) this.setData()
    }
    async auditNo(message: string) {
        const res = await this.request('/nursing_appointment/audit/no', { method: 'POST', data: { id: this.id, rejectRemark: message } })
        if (res) this.setData()
    }
    async assign() {
        const res = await this.request('/nursing_appointment/assign', { method: 'POST', data: { id: this.id, nurseIds: this.nurseIds } })
        this.showAssign = false
        if (res) this.setData()
    }
    async assignNo(message: string) {
        const res = await this.request('/nursing_appointment/assign/no', { method: 'POST', data: { id: this.id, rejectRemark: message } })
        if (res) this.setData()
    }
    async accept() {
        const res = await this.request('/nursing_appointment/accept_by_nurse', { data: { id: this.id } })
        if (res) {
            this.setData()
        }
    }
    async reject(message: string) {
        const res = await this.request('/nursing_appointment/reject_by_nurse', { data: { id: this.id, rejectRemark: message } })
        if (res) {
            this.setData()
        }
    }
    async depart() {
        const res = await this.request('/nursing_appointment/depart', { data: { id: this.id } })
        if (res) {
            this.setData()
        }
    }
    async arrive() {
        const res = await this.request('/nursing_appointment/arrive', { data: { id: this.id } })
        if (res) {
            this.setData()
        }
    }
    async nursing() {
        const res = await this.request('/nursing_appointment/nursing', { data: { id: this.id } })
        if (res) {
            this.setData()
        }
    }
    async complete() {
        const res = await this.request('/nursing_appointment/complete', { data: { id: this.id } })
        if (res) {
            this.setData()
        }
    }
}